<?php

namespace common\jobs;

use Yii;
use yii\console\Exception;
use common\helpers\ArrayHelper;
use common\models\ofc\OfcSeasonStage;
use common\models\ofc\OfcSeasonGroup;
use common\models\ofc\OfcSeasonRound;
use common\models\ofc\OfcTeam;
use common\models\ofc\OfcSeasonTeam;

/**
 * 赛季更新之后执行队列任务
 * Class AfterMatchJob.
 */
class AfterSeasonJob extends BaseRetryableJob {

    public $season = null;
    public $data = [];

    /**
     * @param \yii\queue\Queue $queue which pushed and is handling the job
     */
    public function execute($queue) {
        if (!$this->season || empty($this->data)) {
            throw new Exception('Season is null or empty data.');
        }
        $model = $this->season;
        $data = $this->data;

        $currentStageid = ArrayHelper::getValue($data, 'competition.cur_stage_id');

        Yii::error($data, __CLASS__);
        $stages = ArrayHelper::remove($data, 'stages');

        if ($stages) {
            foreach ($stages as $item) {
                $stageid = ArrayHelper::getValue($item, 'id');
                $stage = OfcSeasonStage::findOrNew([
                    'season_id' => $model->id,
                    'channel_id' => $stageid,
                ]);
                if ($stage->channel_id == $currentStageid) {
                    $stage->status = OfcSeasonStage::STATUS_ACTIVE;
                } else {
                    $stage->status = OfcSeasonStage::STATUS_DEFAULT;
                }
                $stage->name = ArrayHelper::getValue($item, 'name_zh');
                $stage->name_en = ArrayHelper::getValue($item, 'name_en');
                $stage->mode = ArrayHelper::getValue($item, 'mode');
                if (!$stage->save()) {
                    continue;
                }

                $groups = ArrayHelper::getValue($item, 'group_count', 0);

                $charasc = ord('A');
                for ($i = 0; $i < $groups; $i++) {
                    $group = OfcSeasonGroup::findOrNew([
                        'season_id' => $model->id,
                        'stage_id' => $stage->id,
                        'listorder' => $i + 1,
                    ]);
                    $group->name = chr($charasc + $i) . '组';
                    $group->save();
                }

                $rounds = ArrayHelper::getValue($item, 'round_count', 0);

                for ($i = 0; $i < $rounds; $i++) {
                    $order = $i + 1;
                    $round = OfcSeasonRound::findOrNew([
                        'season_id' => $model->id,
                        'stage_id' => $stage->id,
                        'listorder' => $order,
                    ]);
                    $round->name = '第' . $order . '轮';
                    $round->save();
                }
            }
        }

        $teams = ArrayHelper::remove($data, 'teams');
        Yii::error([
            'Update 球队信息',
            $teams
        ], __METHOD__);

        $tempTkv = [];
        foreach ($teams as $item) {
            $cteamid = ArrayHelper::getValue($item, 'id');
            $team = OfcTeam::findOrNew([
                'channel_id' => $cteamid,
            ]);
            $logo = ArrayHelper::getValue($item, 'logo');
            $team->attributes = [
                'name' => ArrayHelper::getValue($item, 'name_zh'),
                'name_en' => ArrayHelper::getValue($item, 'name_en'),
                'logo' => $logo ? '/attachments/ofc/team/' . $logo : '',
            ];
            if ($team->isNewRecord) {
                $team->abbr = ArrayHelper::getValue($item, 'name_zh');
                $team->on($team::EVENT_AFTER_INSERT, [$team, 'updateTeam']);
            } else {
                $team->on($team::EVENT_AFTER_UPDATE, [$team, 'updateTeam']);
            }
            if ($team->save()) {
                $teamid = $team->id;
                $seasonteam = OfcSeasonTeam::findOrNew([
                    'season_id' => $model->id,
                    'team_id' => $teamid,
                ]);
                $seasonteam->save();
//                $tempTkv[$cteamid] = $team;
            } else {
                Yii::error($team->errors, __METHOD__);
            }
        }

//        Yii::$app->queue->push(new AfterMatchJob([
//            'season_id' => $model->id,
//            'matchs' => ArrayHelper::getValue($data, 'matches'),
//            'teams' => $tempTkv,
//        ]));
    }



}
